package com.hzdl.car.contorlle;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Role;
import com.hzdl.car.service.RoleService;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.vo.MenuVo;
import com.hzdl.car.vo.RoleVo;
import com.hzdl.car.vo.TreeNode;
import lombok.extern.apachecommons.CommonsLog;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleContorlle {
    @Autowired
    private RoleService service;

    @RequestMapping("init")
    public HzdlResultJson Init(RoleVo roleVo){
        PageInfo<Role> pageInfo = service.findByWhere(roleVo);
        return new HzdlResultJson(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("roleMenuInit")
    public HzdlResultJson leftMenu(RoleVo roleVo){
        List<TreeNode> list = service.findAll(0, roleVo.getRoleid());
        return new HzdlResultJson(list);
    }

    @RequestMapping("selRolerole")
    public HzdlResultJson selRolerole(RoleVo roleVo,@RequestParam(value = "ids[]") Integer[] ids){
        int row = service.updateRoleMenu(roleVo,ids);
        if(row>0){
            return HzdlResultJson.DISTRIBUTE_SUCCESS();
        }

        return  HzdlResultJson.DISTRIBUTE_FAIL();
    }

    @RequestMapping("add")
    public HzdlResultJson add(RoleVo roleVo){
        int row = service.add1(roleVo);
        if (row>0){
            return new HzdlResultJson("添加成功",null);
        }else {
            return new HzdlResultJson(-1,"添加失败");
        }
    }
    @RequestMapping("update")
    public HzdlResultJson update(RoleVo roleVo){
        int row = service.update(roleVo);
        if (row>0){
            return new HzdlResultJson("修改成功",null);
        }
        return new HzdlResultJson(-1,"修改失败");

    }

    @RequestMapping("delete")
    public HzdlResultJson delete ( RoleVo roleVo){
        int row = service.delete(roleVo.getRoleid());
        if (row>0){
            return HzdlResultJson.DELETE_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_FAIL();
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public HzdlResultJson deleteAll ( RoleVo roleVo,@RequestParam(value = "ids[]") Integer[] ids){
        int row =0;
        for(Integer id:ids){
          row+=service.delete(id);
        }

        if (row>0){
            return HzdlResultJson.DELETE_ALL_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_ALL_FAIL();
        }
    }


}
